<html>
 <head>
  <meta charset="UTF-8">
 </head>
 <body>
  <h1 data-lake-id="oukhb" id="oukhb"><span data-lake-id="u6fe3f285" id="u6fe3f285">典型回答</span></h1>
  <p data-lake-id="uf989b8de" id="uf989b8de"><br></p>
  <p data-lake-id="u2080f644" id="u2080f644"><span data-lake-id="u436e740c" id="u436e740c">当我们在使用Redisson实现分布式锁的时候，会经常用到lock和tryLock两个方法，那么他们有啥区别吗？</span></p>
  <p data-lake-id="uf36b6501" id="uf36b6501"><span data-lake-id="u170d613e" id="u170d613e">​</span><br></p>
  <p data-lake-id="u40f1f91a" id="u40f1f91a"><span data-lake-id="u1bc59671" id="u1bc59671">先来看看代码中是如何解释这两个方法的：</span></p>
  <p data-lake-id="ube2888e7" id="ube2888e7"><span data-lake-id="u4b50d1a0" id="u4b50d1a0">​</span><br></p>
  <pre lang="java"><code>
/**
 * Tries to acquire the lock with defined &lt;code&gt;leaseTime&lt;/code&gt;.
 * Waits up to defined &lt;code&gt;waitTime&lt;/code&gt; if necessary until the lock became available.
 *
 * Lock will be released automatically after defined &lt;code&gt;leaseTime&lt;/code&gt; interval.
 *
 * @param waitTime the maximum time to acquire the lock
 * @param leaseTime lease time
 * @param unit time unit
 * @return &lt;code&gt;true&lt;/code&gt; if lock is successfully acquired,
 *          otherwise &lt;code&gt;false&lt;/code&gt; if lock is already set.
 * @throws InterruptedException - if the thread is interrupted
 */
boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException;

/**
 * Acquires the lock with defined &lt;code&gt;leaseTime&lt;/code&gt;.
 * Waits if necessary until lock became available.
 *
 * Lock will be released automatically after defined &lt;code&gt;leaseTime&lt;/code&gt; interval.
 *
 * @param leaseTime the maximum time to hold the lock after it's acquisition,
 *        if it hasn't already been released by invoking &lt;code&gt;unlock&lt;/code&gt;.
 *        If leaseTime is -1, hold the lock until explicitly unlocked.
 * @param unit the time unit
 *
 */
void lock(long leaseTime, TimeUnit unit);
</code></pre>
  <p data-lake-id="u77b834f3" id="u77b834f3"><br></p>
  <p data-lake-id="u65d281d9" id="u65d281d9"><span data-lake-id="ubcd36c79" id="ubcd36c79">这两个方法声明和注释上，主要由以下3个区别：</span></p>
  <p data-lake-id="ub5552a66" id="ub5552a66"><br></p>
  <ul list="u2441964f">
   <li fid="u24d3a6a6" data-lake-id="udd2ec29b" id="udd2ec29b"><span data-lake-id="ua0e85bf6" id="ua0e85bf6">tryLock</span></li>
  </ul>
  <ul list="u2441964f" data-lake-indent="1">
   <li fid="u24d3a6a6" data-lake-id="u68704250" id="u68704250"><span data-lake-id="u7d625964" id="u7d625964">Tries to acquire the lock with defined leaseTime</span></li>
   <li fid="u24d3a6a6" data-lake-id="ufa0b72f1" id="ufa0b72f1"><span data-lake-id="u4b0486bf" id="u4b0486bf">Waits up to defined waitTime if necessary until the lock became available.</span></li>
   <li fid="u24d3a6a6" data-lake-id="u13eff179" id="u13eff179"><span data-lake-id="u5bfb865d" id="u5bfb865d">返回值为boolean</span></li>
  </ul>
  <ul list="u2441964f" start="2">
   <li fid="u24d3a6a6" data-lake-id="u780d8683" id="u780d8683"><span data-lake-id="u40ee4ed8" id="u40ee4ed8">lock</span></li>
  </ul>
  <ul list="u2441964f" data-lake-indent="1">
   <li fid="u24d3a6a6" data-lake-id="u50fa5068" id="u50fa5068"><span data-lake-id="uac336670" id="uac336670">Acquires the lock with defined leaseTime</span></li>
   <li fid="u24d3a6a6" data-lake-id="u978c41c2" id="u978c41c2"><span data-lake-id="ubdff222b" id="ubdff222b">Waits if necessary until lock became available.</span></li>
   <li fid="u24d3a6a6" data-lake-id="u2b69fc52" id="u2b69fc52"><span data-lake-id="u03b4960c" id="u03b4960c">返回值为void</span></li>
  </ul>
  <p data-lake-id="u43918ef3" id="u43918ef3"><br></p>
  <p data-lake-id="u2e2ee58e" id="u2e2ee58e"><span data-lake-id="u735ee58b" id="u735ee58b">这其实已经把这个两个方法的区别描述的很清楚了。</span></p>
  <p data-lake-id="u950b05c9" id="u950b05c9"><span data-lake-id="uafdba144" id="uafdba144">​</span><br></p>
  <p data-lake-id="u871132e3" id="u871132e3"><span data-lake-id="u4a573a94" id="u4a573a94">那么，介绍一下就是</span><strong><span data-lake-id="uf34769cc" id="uf34769cc">tryLock是尝试获取锁，如果能获取到直接返回true，如果无法获取到锁，他会按照我们指定的waitTime进行阻塞，在这个时间段内他还会再尝试获取锁。如果超过这个时间还没获取到则返回false。如果我们没有指定waitTime，那么他就在未获取到锁的时候，就直接返回false了。</span></strong></p>
  <p data-lake-id="u7471783f" id="u7471783f"><strong><span data-lake-id="u0558ff70" id="u0558ff70">​</span></strong><br></p>
  <pre lang="java"><code>
RLock lock = redisson.getLock("myLock");
boolean isLocked = lock.tryLock(); // 非阻塞方法，立即返回获取结果
if (isLocked) {
    try {
        // 执行临界区代码
    } finally {
        lock.unlock();
    }
} else {
    // 获取锁失败，处理逻辑
}

</code></pre>
  <p data-lake-id="u9694f39e" id="u9694f39e"><strong><span data-lake-id="u45d270fd" id="u45d270fd">​</span></strong><br></p>
  <p data-lake-id="u504e3b6b" id="u504e3b6b"><strong><span data-lake-id="u9de13052" id="u9de13052">lock的原理是以阻塞的方式去获取锁，如果获取锁失败会一直等待，直到获取成功。</span></strong></p>
  <p data-lake-id="uf21468e1" id="uf21468e1"><strong><span data-lake-id="ua6fd7d3d" id="ua6fd7d3d">​</span></strong><br></p>
  <pre lang="java"><code>
RLock lock = redisson.getLock("myLock");
lock.lock(); // 阻塞方法，直到获取到锁
try {
    // 执行代码
} finally {
    lock.unlock();
}

</code></pre>
  <p data-lake-id="uaac01a27" id="uaac01a27"><strong><span data-lake-id="u2da05fb8" id="u2da05fb8">​</span></strong><br></p>
  <p data-lake-id="u6b1996b8" id="u6b1996b8"><strong><span data-lake-id="u2408d244" id="u2408d244">所以，我们可以认为，lock实现的是一个阻塞锁，而tryLock实现的是一个非阻塞锁（在没有指定waitTime的情况下）。</span></strong></p>
  <p data-lake-id="ud6aad951" id="ud6aad951"><br></p>
  <p data-lake-id="u44ea03a1" id="u44ea03a1"><span data-lake-id="uacc47ec0" id="uacc47ec0">​</span><br></p>
  <p data-lake-id="u461f9c32" id="u461f9c32"><span data-lake-id="udd7eb746" id="udd7eb746">​</span><br></p>
 </body>
</html>